var util = require("../../utils/util.js");
var api = require('../../config/api.js');


Page({
  /**
   * 页面的初始数据
   */
  data: {
    schoolList: [],
    activeCategoryId: 1,
    activeTabId: 1,
    schoolyear: '',
    coachyear: '',
    coachList: [],
    userid: '',
    schoolid: '',
    page: 1,
    limit: 30,
    totalPages:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var userid = wx.getStorageSync("userid");
    if (userid == '') {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    } else {
      this.setData({
        userid: userid
      })
    }
    //显示上一年度的结果
    var currentDate = new Date();  
    var previousYear = new Date(currentDate.getFullYear() - 1, currentDate.getMonth(), currentDate.getDate());  
    this.schoolyear = previousYear.getFullYear();  
    this.setData({
      schoolyear: this.schoolyear
    })
    this.coachyear = previousYear.getFullYear(); 
    this.setData({
      coachyear: this.coachyear
    })
      this.getCoachStar();
      this.getSchoolStar();
  },
  tabClick: function (e) {
    var id = e.target.id
    this.setData({
      activeCategoryId: id
    })
  },
  getCoachStar: function () {
    var that = this

    if(that.data.page == 1){
      that.setData({
        coachList: []
      });
    }
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.xykhpmUrlCoach, {
      year: that.coachyear,
      page:that.data.page,
      limit:that.data.limit
    }).then(function (res) {
      //console.log(res)
      that.setData({
        coachList: that.data.coachList.concat(res.data.list),
        totalPages: res.data.pages
      })
      // console.log(res.data.page+"第几页")
      // console.log(that.data.totalPages+"总页数")
      // console.log(res.data.total)
    });
    wx.hideLoading();
  },

  lastyearcoach: function () {
    var that = this;
    that.coachyear--;
    this.setData({
      coachyear: that.coachyear,
      coachList:[],
      page:1
    })
    that.getCoachStar();
  },
  nextyearcoach: function () {
    var that = this;
    that.coachyear++;
    this.setData({
      coachyear: that.coachyear,
      coachList:[],
      page:1
    })
    that.getCoachStar();
  },

  getSchoolStar: function () {
    var that = this
    util.request(api.xykhpmSchoolUrl, {
      year: that.schoolyear
    }).then(function (res) {
      that.setData({
        schoolList: res.data
      })
    });
  },
  lastyearschool: function () {
    var that = this;
    that.schoolyear--;
    this.setData({
      schoolyear: that.schoolyear
    })
    that.getSchoolStar();
  },
  nextyearschool: function () {
    var that = this;
    that.schoolyear++;
    this.setData({
      schoolyear: that.schoolyear
    })
    that.getSchoolStar();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getCoachStar();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})